<?php

namespace App\Admin\Controllers;

use App\Models\WechatTemplateMsg;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class WechatTemplateMsgController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '发送记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatTemplateMsg());

        $grid->column('id', __('ID'));
//        $grid->column('tpl_id', __('模版ID'));
        $grid->column('tpl_name', __('模版标题'));
        $grid->column('tpl_content', __('模版内容'))->display(function ($value) {
            return json_encode($value['data'],JSON_UNESCAPED_UNICODE);
        });
        $grid->column('state', __('发送状态'))->using([1 => '成功', 2 => '失败'])->label([
            1 => 'success',
            2 => 'danger',
        ]);
        $grid->column('openid', __('微信ID'));
        $grid->column('name', __('姓名'));
//        $grid->column('result', __('发送结果'));

        $grid->column('msg', __('异常信息'));
        $grid->column('create_time', __('发送时间'));
//        $grid->column('update_time', __('更新'));
        $grid->disableActions();
        $grid->disableBatchActions();
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->model()->orderBy('id', 'desc');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatTemplateMsg::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('tpl_id', __('模版ID'));
        $show->field('tpl_name', __('模版标题'));
        $show->field('tpl_content', __('模版内容'));
        $show->field('openid', __('微信ID'));
        $show->field('name', __('用户姓名'));
        $show->field('result', __('发送结果'));
        $show->field('state', __('发送状态'));
        $show->field('msg', __('异常信息'));
        $show->field('create_time', __('发送时间'));
        $show->field('update_time', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatTemplateMsg());

        $form->display('tpl_id', __('Tpl id'));
        $form->display('tpl_name', __('Tpl name'));
        $form->display('tpl_content', __('Tpl content'));
        $form->display('openid', __('Openid'));
        $form->display('name', __('Name'));

        return $form;
    }
}
